

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>speechemotionrecognition.utilities &mdash; Speech Emotion Recognition 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../main.html" class="icon icon-home"> Speech Emotion Recognition
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../modules.html">speechemotionrecognition</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../main.html">Speech Emotion Recognition</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../main.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
          <li><a href="../speechemotionrecognition.html">speechemotionrecognition</a> &raquo;</li>
        
      <li>speechemotionrecognition.utilities</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for speechemotionrecognition.utilities</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>

<span class="sd">author: harry-7</span>

<span class="sd">This file contains functions to read the data files from the given folders and</span>
<span class="sd">generate Mel Frequency Cepestral Coefficients features for the given audio</span>
<span class="sd">files as training samples.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="k">import</span> <span class="n">Tuple</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">scipy.io.wavfile</span> <span class="k">as</span> <span class="nn">wav</span>
<span class="kn">from</span> <span class="nn">speechpy.feature</span> <span class="k">import</span> <span class="n">mfcc</span>

<span class="n">mean_signal_length</span> <span class="o">=</span> <span class="mi">32000</span>  <span class="c1"># Empirically calculated for the given data set</span>


<div class="viewcode-block" id="get_feature_vector_from_mfcc"><a class="viewcode-back" href="../../speechemotionrecognition.utilities.html#speechemotionrecognition.utilities.get_feature_vector_from_mfcc">[docs]</a><span class="k">def</span> <span class="nf">get_feature_vector_from_mfcc</span><span class="p">(</span><span class="n">file_path</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">flatten</span><span class="p">:</span> <span class="nb">bool</span><span class="p">,</span>
                                 <span class="n">mfcc_len</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">39</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Make feature vector from MFCC for the given wav file.</span>

<span class="sd">    Args:</span>
<span class="sd">        file_path (str): path to the .wav file that needs to be read.</span>
<span class="sd">        flatten (bool) : Boolean indicating whether to flatten mfcc obtained.</span>
<span class="sd">        mfcc_len (int): Number of cepestral co efficients to be consider.</span>

<span class="sd">    Returns:</span>
<span class="sd">        numpy.ndarray: feature vector of the wav file made from mfcc.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">fs</span><span class="p">,</span> <span class="n">signal</span> <span class="o">=</span> <span class="n">wav</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">file_path</span><span class="p">)</span>
    <span class="n">s_len</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">signal</span><span class="p">)</span>
    <span class="c1"># pad the signals to have same size if lesser than required</span>
    <span class="c1"># else slice them</span>
    <span class="k">if</span> <span class="n">s_len</span> <span class="o">&lt;</span> <span class="n">mean_signal_length</span><span class="p">:</span>
        <span class="n">pad_len</span> <span class="o">=</span> <span class="n">mean_signal_length</span> <span class="o">-</span> <span class="n">s_len</span>
        <span class="n">pad_rem</span> <span class="o">=</span> <span class="n">pad_len</span> <span class="o">%</span> <span class="mi">2</span>
        <span class="n">pad_len</span> <span class="o">//=</span> <span class="mi">2</span>
        <span class="n">signal</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">pad</span><span class="p">(</span><span class="n">signal</span><span class="p">,</span> <span class="p">(</span><span class="n">pad_len</span><span class="p">,</span> <span class="n">pad_len</span> <span class="o">+</span> <span class="n">pad_rem</span><span class="p">),</span>
                        <span class="s1">&#39;constant&#39;</span><span class="p">,</span> <span class="n">constant_values</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">pad_len</span> <span class="o">=</span> <span class="n">s_len</span> <span class="o">-</span> <span class="n">mean_signal_length</span>
        <span class="n">pad_len</span> <span class="o">//=</span> <span class="mi">2</span>
        <span class="n">signal</span> <span class="o">=</span> <span class="n">signal</span><span class="p">[</span><span class="n">pad_len</span><span class="p">:</span><span class="n">pad_len</span> <span class="o">+</span> <span class="n">mean_signal_length</span><span class="p">]</span>
    <span class="n">mel_coefficients</span> <span class="o">=</span> <span class="n">mfcc</span><span class="p">(</span><span class="n">signal</span><span class="p">,</span> <span class="n">fs</span><span class="p">,</span> <span class="n">num_cepstral</span><span class="o">=</span><span class="n">mfcc_len</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">flatten</span><span class="p">:</span>
        <span class="c1"># Flatten the data</span>
        <span class="n">mel_coefficients</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">mel_coefficients</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">mel_coefficients</span></div>


<div class="viewcode-block" id="get_data"><a class="viewcode-back" href="../../speechemotionrecognition.utilities.html#speechemotionrecognition.utilities.get_data">[docs]</a><span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="n">data_path</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">flatten</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span> <span class="n">mfcc_len</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">39</span><span class="p">,</span>
             <span class="n">class_labels</span><span class="p">:</span> <span class="n">Tuple</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;Neutral&quot;</span><span class="p">,</span> <span class="s2">&quot;Angry&quot;</span><span class="p">,</span> <span class="s2">&quot;Happy&quot;</span><span class="p">,</span> <span class="s2">&quot;Sad&quot;</span><span class="p">))</span> <span class="o">-&gt;</span> \
        <span class="n">Tuple</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]:</span>
    <span class="sd">&quot;&quot;&quot;Extract data for training and testing.</span>

<span class="sd">    1. Iterate through all the folders.</span>
<span class="sd">    2. Read the audio files in each folder.</span>
<span class="sd">    3. Extract Mel frequency cepestral coefficients for each file.</span>
<span class="sd">    4. Generate feature vector for the audio files as required.</span>

<span class="sd">    Args:</span>
<span class="sd">        data_path (str): path to the data set folder</span>
<span class="sd">        flatten (bool): Boolean specifying whether to flatten the data or not.</span>
<span class="sd">        mfcc_len (int): Number of mfcc features to take for each frame.</span>
<span class="sd">        class_labels (tuple): class labels that we care about.</span>

<span class="sd">    Returns:</span>
<span class="sd">        Tuple[numpy.ndarray, numpy.ndarray]: Two numpy arrays, one with mfcc and</span>
<span class="sd">        other with labels.</span>


<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">data</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">labels</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">names</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">cur_dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;curdir: </span><span class="si">%s</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">cur_dir</span><span class="p">)</span>
    <span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="n">data_path</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">directory</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">class_labels</span><span class="p">):</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;started reading folder </span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">directory</span><span class="p">)</span>
        <span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="n">directory</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">):</span>
            <span class="n">filepath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()</span> <span class="o">+</span> <span class="s1">&#39;/&#39;</span> <span class="o">+</span> <span class="n">filename</span>
            <span class="n">feature_vector</span> <span class="o">=</span> <span class="n">get_feature_vector_from_mfcc</span><span class="p">(</span><span class="n">file_path</span><span class="o">=</span><span class="n">filepath</span><span class="p">,</span>
                                                          <span class="n">mfcc_len</span><span class="o">=</span><span class="n">mfcc_len</span><span class="p">,</span>
                                                          <span class="n">flatten</span><span class="o">=</span><span class="n">flatten</span><span class="p">)</span>
            <span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">feature_vector</span><span class="p">)</span>
            <span class="n">labels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
            <span class="n">names</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;ended reading folder </span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">directory</span><span class="p">)</span>
        <span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="s1">&#39;..&#39;</span><span class="p">)</span>
    <span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="n">cur_dir</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">labels</span><span class="p">)</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Hemanth Kumar Veeranki

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>